package com.wxx.house;

import com.wxx.push.WeChatPush;
import com.wxx.util.CrawlerUtil;
import com.wxx.util.MyExceptionHandler;
import com.wxx.util.exception.IExceptionHandler;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.processor.PageProcessor;

import java.util.ArrayList;
import java.util.List;

/**
 * 获取自定义城市的平均房价
 */
public class HousePriceProcess implements PageProcessor {

    public void process(Page page) {
       // 无需在这处理，在下载器里已经处理
    }

    public Site getSite() {
        return Site.me().setRetryTimes(10).setSleepTime(100);
    }

    public void start() {
        List<String> taskList = new ArrayList<>();
        taskList.add("https://sz.fang.ke.com/loupan/baoanqu-futianqu-longgangqu-longhuaqu-luohuqu-nanshanqu/l3");   // 深圳
        taskList.add("https://lz.fang.ke.com/loupan/xiguqu-anningqu-chengguanqu-qilihequ-gaolanxian/l3");   // 兰州
        taskList.add("https://gz.fang.ke.com/loupan/yuexiu-haizhu-tianhe-baiyun-huangpugz-panyu-huadou-nansha/l3");   // 广州
        taskList.add("https://fs.fang.ke.com/loupan/l3");   // 佛山
        taskList.add("https://wh.fang.ke.com/loupan/caidian-jiangxia-jiangan-jianghan-wuchang-hanyang-hongshan-donghugaoxin/l3");   // 武汉

        HousePriceDownloader downloader = new HousePriceDownloader();
        Spider spider = CrawlerUtil.genSpider(this, downloader);
        for (String url: taskList) {
            spider.addUrl(url);
        }
        spider.run();
        WeChatPush.pushText("房价获取任务完成：" + downloader.resultList);
    }

    public static void main(String[] args) {
        MyExceptionHandler.getInstance().setDefaultUnCacheExceptionHandler();
        MyExceptionHandler.getInstance().addExceptionListener(new IExceptionHandler() {
            @Override
            public void handler(Thread thread, Throwable throwable) {
                System.out.println("出异常了：" + throwable.getMessage());
                throwable.printStackTrace();
            }
        });
        new HousePriceProcess().start();
    }

}